package com.hssy.stationtv;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
	
	public static final String FILENAME = "setting.set";
	
	public static final String station = "station.set";

	public static final String loadPage = "loadPage.set";
	
	public static String TelevisionID = "";
	
	public static boolean firstStart = true;
	
	protected void onCreate(Bundle paramBundle){
	    super.onCreate(paramBundle);
	    TelevisionID = getCPUSerial();
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
	            WindowManager.LayoutParams.FLAG_FULLSCREEN);
	    setContentView(R.layout.activity_main);
    	upgrade();
    	if(firstStart) {
    		if(!readModularName().equals("")) {
    			if(readModularName().equals("realTimeStatus")) {
                	Intent localIntent = new Intent(MainActivity.this, ButtonOne.class);
            		MainActivity.this.startActivity(localIntent);
    			} else if(readModularName().equals("chargingRecord")) {
    				Intent localIntent = new Intent(MainActivity.this, ButtonTwo.class);
            		MainActivity.this.startActivity(localIntent);
    			} else if(readModularName().equals("realRecord")) {
    				Intent localIntent = new Intent(MainActivity.this, ButtonFour.class);
            		MainActivity.this.startActivity(localIntent);
    			}
    		}
    		firstStart = false;
    	}
	}
	
	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent){
//		reStart();
		if ((paramInt == 23) || (paramInt == 19) || (paramInt == 20) || (paramInt == 21) || (paramInt == 22)){
			if(readFiles().equals("1")) {

				CustomDialog.Builder builder = new CustomDialog.Builder(this);
				builder.setPositiveButton("实时状态", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						Intent localIntent = new Intent(MainActivity.this, ButtonOne.class);
						MainActivity.this.startActivity(localIntent);
					}
				});
				builder.setNegativeButton("记录统计", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						Intent localIntent = new Intent(MainActivity.this, ButtonTwo.class);
						MainActivity.this.startActivity(localIntent);
					}
				});
				builder.setSettingButton("IP设置", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						Intent localIntent = new Intent(MainActivity.this, ButtonThree.class);
						MainActivity.this.startActivity(localIntent);
					}
				});
				builder.create().show();
			} else {

				StationTwoDialog.Builder builder = new StationTwoDialog.Builder(this);
				builder.setPositiveButton("记录统计", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						Intent localIntent = new Intent(MainActivity.this, ButtonFour.class);
						MainActivity.this.startActivity(localIntent);
					}
				});
				builder.setNegativeButton("IP设置", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						Intent localIntent = new Intent(MainActivity.this, ButtonThree.class);
						MainActivity.this.startActivity(localIntent);
					}
				});
				builder.create().show();
			}
		}
		return super.onKeyDown(paramInt, paramKeyEvent);
	}
	
	public String readFiles() {
		String content = null;
		try {
			FileInputStream fis = openFileInput(MainActivity.station);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			content = baos.toString();
			fis.close();
			baos.close();
		} catch (FileNotFoundException e) {
            e.printStackTrace();
            writeFiles("1");
            content = "1";
        } catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}
	
	public void upgrade() {
	    new Thread(new Runnable(){
            @Override
            public void run() {
    			Looper.prepare();
            	UpdateManager manager = new UpdateManager(MainActivity.this);
    			manager.checkUpdate("main");
    			Looper.loop();
            }
        }).start();
	}
	
	private void writeFiles(String content) {
		try {
			FileOutputStream fos = openFileOutput(MainActivity.station,
			Context.MODE_PRIVATE);
			fos.write(content.getBytes());
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 读取失败为"0000000000000000"
	*/
	public String getCPUSerial() {

		String cpuAddress = "0000000000000000";
		cpuAddress = Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID_ID);
	    return cpuAddress;
	}
	
	//读取默认加载页
	public String readModularName() {
		String content = null;
		try {
			FileInputStream fis = openFileInput(MainActivity.loadPage);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			content = baos.toString();
			fis.close();
			baos.close();
		} catch (FileNotFoundException e) {
            e.printStackTrace();
            content = "";
        } catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}
}